package com.k2data.k2app.service.pas04;


import com.k2data.k2app.HttpClientUtil;
import com.k2data.k2app.common.MyConstant;
import com.k2data.k2app.exception.K2ResponseException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 从pas取回来的数据
 * Created by clb on 17-3-30.
 */
@Log4j2
@Service
public class Pas04Basic {
	@Autowired
	private MyConstant myConstant;

	public String getJobName (){
		return "k2asset_copy";
//		return myConstant.getJobName();
	}

	public String getProjectUrl (){
		return myConstant.getService()+"projects";
	}
	public String getRunJobUrl(){
		return myConstant.getService()+"projects/run/";
	}

	public String getJobsUrl(){
		return myConstant.getService()+"jobs";
	}
	public String getResultUrl(){
		return myConstant.getService()+"results";
	}
	public String getDownLoadUrl(Long modelId,String fileName){
		return myConstant.getService()+"file/download?projId="+modelId+"&fileName="+fileName;
	}
	public String getHdfsDownLoadUrl(String filepath,String fileName){
		return myConstant.getService()+"hdfs/download?filePath="+filepath+"&fileName="+fileName;
	}
	public String getHdfsUrl(){
		return myConstant.getService()+"hdfs/browse";
	}


	/**
	 * 从条件中抽取出设备信息
	 * @param whereCondition
	 * @return
	 */
	protected String getDeviceNoFromCondition(String whereCondition){
		char[] c = whereCondition.toCharArray();
		int start = 0 ,end = 0, count = 0;
		for(int i =0 ;i<c.length; i++){
			if(c[i] == '\''){
				if(count == 0){
					count ++;
					start = i;
				}else{
					end = i;
					break;
				}
			}
		}
		return whereCondition.substring(start+1,end);
	}
	/**
	 * 拼接pas url
	 * @param url
	 * @return
	 */
	public String getURI(String url) throws K2ResponseException {
		return myConstant.getURI(url);
	}

	/**
	 * 拼接查询条件
	 * @param params
	 * @return
	 */
	public String getQueryCondition(Map<String,Object> params){
		return myConstant.getQueryCondition(params);
	}

	/**
	 * 从pas获取数据
	 * 验证token过期,重新带token访问
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public String getFromPas(String url) throws Exception{
		String infoError = HttpClientUtil.onGet(url);
		if(infoError.startsWith("<!DOCTYPE html>")||infoError.startsWith("<html>")){
			infoError = HttpClientUtil.onGet(getURI(url));
		}
		return infoError;
	}
}
